package com.ctshk.app.system.controller;


import java.util.List;

import javax.validation.Valid;

import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.system.dto.QueryChildrenDeptDTO;
import com.ctshk.rpc.system.dto.SysDepartmentDTO;
import com.ctshk.rpc.system.dto.SysDepartmentEmployeeDTO;
import com.ctshk.rpc.system.req.SysDepartmentReq;
import com.ctshk.rpc.system.service.ISysDepartmentService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 组织架构 前端控制器
 * </p>
 *
 * @author 谢诗宏
 * @since 2020-12-22
 */
@Api(tags="组织架构")
@RestController
@RequestMapping("/department")
public class SysDepartmentController extends BaseController {

    @DubboReference
    private ISysDepartmentService sysDepartmentService;

    @ApiOperation(value="新增组织架构", notes="新增组织架构")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.ADD, operTitle = "新增组织架构", operDesc = "新增组织架构")
    @PostMapping("/add")
    public Result add(@RequestBody @Valid SysDepartmentReq sysDepartmentReq) {
        sysDepartmentService.add(sysDepartmentReq, getCurrentUser(getRequest()).getId());
        return Result.success();
    }

    @ApiOperation(value="删除组织架构", notes="删除组织架构")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.DELETE, operTitle = "删除组织架构", operDesc = "删除组织架构")
    @GetMapping("/del/{id}")
    public Result del(@PathVariable long id) {
        sysDepartmentService.del(id, getCurrentUser(getRequest()).getId());
        return Result.success();
    }

    @ApiOperation(value="编辑组织架构", notes="编辑组织架构")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.UPDATE, operTitle = "编辑组织架构", operDesc = "编辑组织架构")
    @PostMapping("/update")
    public Result update(@RequestBody @Valid SysDepartmentReq sysDepartmentReq) {
        sysDepartmentService.update(sysDepartmentReq, getCurrentUser(getRequest()).getId());
        return Result.success();
    }

    @ApiOperation(value="查询组织架构", notes="查询组织架构")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "查询组织架构", operDesc = "查询组织架构")
    @GetMapping("/query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" , value = "组织部门id"),
            @ApiImplicitParam(name = "isAll" , value = "true全部，false有效")
    })
    public Result<List<SysDepartmentDTO>> query(@RequestParam(name = "id", defaultValue = "0") Long id,@RequestParam(name = "isAll", defaultValue = "false") boolean isAll) {
        return Result.success(sysDepartmentService.query(id, getCurrentUser(getRequest()).getId(),isAll));
    }

    @ApiOperation(value="设置组织架构是否有效", notes="设置组织架构是否有效")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.UPDATE, operTitle = "设置组织架构是否有效", operDesc = "设置组织架构是否有效")
    @GetMapping("/set-status")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "组织id"),
            @ApiImplicitParam(name = "status", value = "状态0=无效;1有效")
    })
    public Result setStatus(@RequestParam(name = "id") Long id, @RequestParam(name = "status") int status) {
        sysDepartmentService.setStatus(id, status, getCurrentUser(getRequest()).getId());
        return Result.success();
    }

    @ApiOperation(value="查询组织部门员工", notes="查询组织架构+部门员工")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "查询组织部门员工", operDesc = "查询组织架构+部门员工")
    @GetMapping("/query/dept-employees")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "searchName", value = "组织部门名称或者员工姓名"),
            @ApiImplicitParam(name = "type", value = "all:查询部门+员工;dept:只查询部门")
    })
    public Result<List<SysDepartmentEmployeeDTO>> queryDeptEmployees(@RequestParam(name = "searchName", required = false) String searchName, @RequestParam(name = "type", defaultValue = "all", required = false) String type) {
        return Result.success(sysDepartmentService.queryDeptEmployees(searchName, type));
    }

    @ApiOperation(value="查询下级部门", notes="查询下级部门")
    @OperationLogAnnotation(operModul = OperationLogModel.SYSTEM, operType = OperationLogType.QUERY, operTitle = "查询下级部门", operDesc = "查询下级部门")
    @GetMapping("/queryChildrenDept")
    public Result<List<QueryChildrenDeptDTO>> queryChildrenDept() {
        return sysDepartmentService.queryChildrenDept();
    }
}
